package com.yang.dao;

import com.yang.entity.Address;

import java.sql.*;
import java.util.*;

public class AddressDao {
    //根据用户idchaxun
    public List<Address> queryByUserId(int userId){
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Address> list = new ArrayList<>();
        try {
            ps=conn.prepareStatement("select * from address where userid=?");
            ps.setInt(1, userId);
            rs=ps.executeQuery();
            while(rs.next()){
                Address obj = new Address();
                obj.setId(rs.getInt("id"));
                obj.setContacts(rs.getString("contacts"));
                obj.setAddress(rs.getString("address"));
                obj.setTel(rs.getString("tel"));
                obj.setUserid(rs.getInt("userid"));
                obj.setDef(rs.getInt("def"));
                list.add(obj);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.close(rs,ps,conn);
        }
        return list;
    }

    //根据id删除
    public int deleteByUserId(int id){
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        int sum=0;
        try {
            ps=conn.prepareStatement("delete from address where id=?");
            ps.setInt(1, id);
            sum=ps.executeUpdate();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.close(null,ps,conn);
        }
        return sum;
    }
    //添加收获地址
    //添加地址
    public int saveAddress(Address addr) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("insert into address(userid,def,contacts,tel,address) values(?, ?, ?, ?, ?)");
            ps.setInt(1, addr.getUserid());
            ps.setInt(2, addr.getDef());
            ps.setString(3, addr.getContacts());
            ps.setString(4, addr.getTel());
            ps.setString(5, addr.getAddress());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //根据id查询地址
    public Address queryAddressById(int id){
        Connection conn= BaseDao.getConn();
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps=conn.prepareStatement("select * from address where id=?");
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while(rs.next()){
                Address addr=new Address();
                addr.setAddress(rs.getString("Address"));
                addr.setId(rs.getInt("Id"));
                addr.setContacts(rs.getString("Contacts"));
                addr.setDef(rs.getInt("Def"));
                addr.setTel(rs.getString("Tel"));
                addr.setUserid(rs.getInt("Userid"));
                return addr;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.close(rs,ps,conn);
        }
        return null;
    }
}
